package com.ja.openchat.ui.chat.adapter.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.R;
import com.ja.openchat.ui.chat.FileDisplayActivity;
import com.ja.openchat.views.AppFileReaderView;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.file.DocumentInfo;
import com.zy.openchat.core.model.message.MessageContent;
import com.zy.openchat.core.model.message.UiMessage;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.zy.openchat.util.FileUtils;
import com.ycbjie.webviewlib.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

@MessageContentType(TelegramManager.MessageType.MessageType_Document)
@EnableContextMenu
public class FileMessageContentViewHolder extends MediaMessageContentViewHolder{

    @BindView(R.id.fileIconImageView)
    ImageView fileIconImageView;
    @BindView(R.id.fileNameTextView)
    TextView nameTextView;
    @BindView(R.id.fileSizeTextView)
    TextView sizeTextView;

    public FileMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    protected void onBind(UiMessage message) {
        super.onBind(message);
        MessageContent imageMessage = message.message.content;
        if(imageMessage==null){
            return;
        }
        DocumentInfo documentInfo = imageMessage.document;
        nameTextView.setText(imageMessage.title);
        fileIconImageView.setImageResource(DocumentInfo.fileTypeIcon(documentInfo.file_name));
        if(documentInfo.document==null){
            return;
        }
        sizeTextView.setText(FileUtils.getReadableFileSize(documentInfo.document.expected_size));
        if(!documentInfo.isFileDownloaded()||!documentInfo.isFileDownloadCompleted()){
            TelegramManager.getTelegramManager().downloadFile(message.message.chat_id+"_" + message.message.id, documentInfo.document.id, 0, TelegramManager.FileType.FileType_Message_Document);
        }
    }

    @OnClick(R.id.imageView)
    public void onClick(View view) {
        if(message.message.content==null){
            return;
        }
        DocumentInfo documentInfo = message.message.content.document;
        if(!documentInfo.isFileDownloaded()||!documentInfo.isFileDownloadCompleted()){
            ToastUtils.showToast(fragment.getString(R.string.file_downloading_text));
            return;
        }
        if(DocumentInfo.isImageFile(documentInfo.file_name)||DocumentInfo.isVideoFile(documentInfo.file_name)){
            previewMM();
        }else{
            String fileExtension = AppFileReaderView.getFileType(documentInfo.file_name);
            FileDisplayActivity.startActivity(fragment.getContext(),documentInfo.localFilePath(),fileExtension);
        }
    }
}
